<?php
/**
 * AdminModel.class.php     系统管理员模型
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    2012-03-21 14:26:32
 * @last modify             2012-03-21 14:26:32 by mashanling
 */

!defined('ROOT') && exit('Access Denied');

class AdminModel extends CommonModel {
    protected $tableName = 'sys_admin';
    protected $pk = 'said';
    protected $db_fields = array (
        'said'          => array('filter' => array('int')),
        'sa_user'       => array('validate' => array(array('require', '请输入用户名'), array('', '用户名已经存在', Model::EXISTS_VALIDATE, 'unique'))),
        'sa_pswd'       => array('validate' => array('checkPassword', '请输入密码', Model::MUST_VALIDATE, 'callback')),
        'real_name'     => array('validate' => array('require', '请输入真实姓名')),
        'last_ip'       => array('filter' => array('ip')),
        'add_time'      => array('filter' => array('int')),
        'last_time'     => array('filter' => array('int')),
        'group_id'      => array('filter' => array('int'), array('require', '请选择管理组')),
        'logins'        => array('filter' => array('int')),
        'hd_cpuinfo'    => null,
        'hd_macadd'     => null,
        'hd_sysname'    => null,
        'hd_device'     => null,
        'is_check_hd'   => array('filter' => array('int')),
        'mac_address'   => null,
        'category_priv' => null,
    );

    function getList() {
        $data  = $this->order('said DESC')->select(array(), 'said');
        return $data;
    }

    function getInfo($id) {
        //var_dump($this->field('said,sa_pswd', true)->limit(1)->select(), $this);
        return $this->find($id);
    }

    function checkPassword($pwd, $data, $confirm = 0) {
        $pwd = trim($pwd);
        $id  = Param::post('said', 'int');

        if ($id && $confirm) {//编辑，确认密码
            $original_pwd = Param::post('sa_pwd');
            return $pwd == '' ? true : $pwd == $original_pwd;
        }
        elseif (!$id && $pwd == '') {
            return $confirm ? '请输入确认密码' : false;
        }
        elseif (!$id && $confirm && $pwd != Param::post('sa_pswd')) {
            return false;
        }

        return true;
    }
}